//
//  YAMainViewController.swift
//  于昂微博
//
//  Created by 鱼龙之梦 on 15/8/31.
//  Copyright © 2015年 鱼龙之梦. All rights reserved.
//

import UIKit

class YAMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        // Do any additional setup after loading the view.
    }
    private func addChildViewControllers() {
        addChildViewController(YAHomeController(),title:"首页",imageName:"tabbar_home")
        addChildViewController(YAMessageController(),title:"消息",imageName:"tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(YADiscoverController(),title:"发现",imageName:"tabbar_discover")
        addChildViewController(YAProfileController(),title:"我",imageName:"tabbar_profile")
    }
    private func addChildViewController(childController: UIViewController,title:String,imageName:String) {
        childController.navigationItem.titleView = UILabel(title: title, color: UIColor.darkGrayColor(), fontSize: 18)
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: childController)
        addChildViewController(nav)
    }
    private func setupComposedButton(){
        let count = childViewControllers.count
        let w = tabBar.bounds.width/CGFloat(count)
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composedButton.frame = CGRectOffset(rect, 2*w, 0)
    
    }
    //加号按钮
    private lazy var composedButton:UIButton = {
            let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"),forState:UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"),forState:UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        self.tabBar.addSubview(btn)
        return btn
    }()

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setupComposedButton()
    }
    //按钮的点击事件
    @objc private func clickComposedButton()
    {
        let vc:UIViewController
        //根据用户是否登录过去判断
        if YAUserAcountVM.sharedUserAccount.userLogon {
            vc = YAComposeController()
        }
        else
        {
            vc = YALoginController()
        }
        let nv = UINavigationController(rootViewController: vc)
        presentViewController(nv, animated: true, completion: nil)
    }
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
    }
}
